package com.paireach.bw.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.BaseEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.utils.CustomProgressDialogUtil;
import com.paireach.bw.utils.ProgressDialogUtil;
import com.paireach.bw.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import es.dmoral.toasty.Toasty;


/**
 * BaseActivity added eventbus
 * <p> register eventbus in OnCreate and unregister in onDestroy</p>
 */
public class BaseActivity extends AppCompatActivity {

    public Context context;
    public String phoneNumber;
    public String name;
    public String driverNum;//车牌号
    public ProgressDialogUtil progressDialogUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        phoneNumber = MyApplication.getSpUtils().getString(Constant.TEL, "");
        name = MyApplication.getSpUtils().getString(Constant.REAL_NAME, "");
        driverNum = MyApplication.getSpUtils().getString(Constant.DRIVER_NUM, "");

        EventBus.getDefault().register(this);
        progressDialogUtil = new ProgressDialogUtil(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public final void onEventBack(BaseEvent event) {
        if (event instanceof HttpErrorEvent) {
            //mark error
            httpErrorEvent((HttpErrorEvent) event);
        } else if (event instanceof HttpSuccessEvent) {
            httpSuccessEvent((HttpSuccessEvent) event);
        } else {
            applicationEvent((AppEvent) event);
        }
    }

    /**
     * 处理网络失败/错误请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 错误事件
     */
    public void httpErrorEvent(HttpErrorEvent event) {

    }

    /**
     * 处理网络成功请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 成功事件
     */
    public void httpSuccessEvent(HttpSuccessEvent event) {

    }

    /**
     * 处理app内部事件
     *
     * @param event app内部事件
     */
    public void applicationEvent(AppEvent event) {

    }

    public void showToast(String message) {
        ToastUtil.showShortToast(this, message);
    }

    public void showInfo(String message) {
        if (TextUtils.isEmpty(message)) {
            Toasty.info(this, "系统错误").show();
//            Toast toast = Toasty.info(this, "系统错误");
//            toast.setGravity(Gravity.CENTER,0,0);
//            toast.show();
        } else {
            Toasty.info(this, message).show();
        }
    }

    public void showSuccess(String message) {
        if (TextUtils.isEmpty(message)) {
            Toasty.success(this, "系统错误").show();
        } else {
            Toasty.success(this, message).show();
        }
    }

    public void showWarning(String message) {
        if (TextUtils.isEmpty(message)) {
            Toasty.warning(this, "系统错误").show();
        } else {
            Toasty.warning(this, message).show();
        }
    }


    public void showError(String message) {
        if (TextUtils.isEmpty(message)) {
            Toasty.warning(this, "系统错误").show();
        } else {
            Toasty.warning(this, message).show();
        }
    }

    public void showProgressDialog() {
        progressDialogUtil.showDialog();
    }

    public void dismissProgressDialog() {
        progressDialogUtil.dismissDialog();
    }
}
